/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.eclipse.iota.model;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.iota.IotaPackage;
import org.eclipse.iota.util.IotaEqualityUtil;
import org.eclipse.iota.util.IotaProofAssistant;

/**
 * <!-- begin-user-doc -->
 * A representation of the model object '<em><b>Proof</b></em>'.
 * <!-- end-user-doc -->
 *
 * @see org.eclipse.iota.IotaPackage#getProof()
 * @model kind="class"
 * @generated
 */
public class Proof extends EObjectImpl implements Element {
	
	/**
	 * An exception class for proof errors.
	 * @author Christian Krause
	 * @generated NOT
	 */
	public static class ProofException extends Exception {

		// Default serial ID
		private static final long serialVersionUID = 1L;
		
		// Step that caused the problem.
		private Step step;
		
		public ProofException(String message, Step step, Throwable cause) {
			super(step!=null ? message + ": " + step : message, cause);
			this.step = step;
		}
		
		public Step getStep() {
			return step;
		}
		
	}
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @model
	 * @generated NOT
	 */
	public void validate() throws ProofException {
		
		// Some basic checks:
		if (getSequent()==null) {
			throw new ProofException("Proof not part of a sequent", null, null);
		}
		if (getSequent().getPremise()==null) {
			throw new ProofException("Missing premise", null, null);
		}
		if (getSequent().getConclusion()==null) {
			throw new ProofException("Missing conclusion", null, null);
		}
		
		// Compute the result:
		Property result = IotaProofAssistant.getIntermediateResult(getSequent(), getSteps().size()-1);
		Property conclusion = IotaProofAssistant.getConclusionAsProperty(getSequent());
		
		// Now the result should be the same as the conclusion:
		if (!IotaEqualityUtil.equals(result, conclusion)) {
			Step last = getSteps().isEmpty() ? null : getSteps().get(getSteps().size()-1);
			throw new ProofException("Proof does not yield the expected conclusion", last, null);
		}
		
	}

	/**
	 * @generated NOT
	 */
	@Override
	public String toString() {
		String result = "proof\n";
		for (Step step : getSteps()) {
			result = result + "\t" + step + "\n";
		}
		return result + ";\n";
	}
	
	/* ---------------------------------------------------------------- *
	 * GENERATED CODE.                                                  *
	 * Do not edit below this line. If you need to edit, move it above  *
	 * this line and change the '@generated'-tag to '@generated NOT'.   *
	 * ---------------------------------------------------------------- */	

	/**
	 * The cached value of the '{@link #getSteps() <em>Steps</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSteps()
	 * @generated
	 * @ordered
	 */
	protected EList<Step> steps;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Proof() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return IotaPackage.Literals.PROOF;
	}

	/**
	 * Returns the value of the '<em><b>Steps</b></em>' containment reference list.
	 * The list contents are of type {@link org.eclipse.iota.model.Step}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Steps</em>' containment reference list.
	 * @see org.eclipse.iota.IotaPackage#getProof_Steps()
	 * @model containment="true"
	 * @generated
	 */
	public EList<Step> getSteps() {
		if (steps == null) {
			steps = new EObjectContainmentEList<Step>(Step.class, this, IotaPackage.PROOF__STEPS);
		}
		return steps;
	}

	/**
	 * Returns the value of the '<em><b>Sequent</b></em>' container reference.
	 * It is bidirectional and its opposite is '{@link org.eclipse.iota.model.Sequent#getProof <em>Proof</em>}'.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Sequent</em>' container reference.
	 * @see #setSequent(Sequent)
	 * @see org.eclipse.iota.IotaPackage#getProof_Sequent()
	 * @see org.eclipse.iota.model.Sequent#getProof
	 * @model opposite="proof" transient="false"
	 * @generated
	 */
	public Sequent getSequent() {
		if (eContainerFeatureID() != IotaPackage.PROOF__SEQUENT) return null;
		return (Sequent)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetSequent(Sequent newSequent, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newSequent, IotaPackage.PROOF__SEQUENT, msgs);
		return msgs;
	}

	/**
	 * Sets the value of the '{@link org.eclipse.iota.model.Proof#getSequent <em>Sequent</em>}' container reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Sequent</em>' container reference.
	 * @see #getSequent()
	 * @generated
	 */
	public void setSequent(Sequent newSequent) {
		if (newSequent != eInternalContainer() || (eContainerFeatureID() != IotaPackage.PROOF__SEQUENT && newSequent != null)) {
			if (EcoreUtil.isAncestor(this, newSequent))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newSequent != null)
				msgs = ((InternalEObject)newSequent).eInverseAdd(this, IotaPackage.SEQUENT__PROOF, Sequent.class, msgs);
			msgs = basicSetSequent(newSequent, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, IotaPackage.PROOF__SEQUENT, newSequent, newSequent));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.PROOF__SEQUENT:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetSequent((Sequent)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.PROOF__SEQUENT:
				return basicSetSequent(null, msgs);
			case IotaPackage.PROOF__STEPS:
				return ((InternalEList<?>)getSteps()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case IotaPackage.PROOF__SEQUENT:
				return eInternalContainer().eInverseRemove(this, IotaPackage.SEQUENT__PROOF, Sequent.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case IotaPackage.PROOF__SEQUENT:
				return getSequent();
			case IotaPackage.PROOF__STEPS:
				return getSteps();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case IotaPackage.PROOF__SEQUENT:
				setSequent((Sequent)newValue);
				return;
			case IotaPackage.PROOF__STEPS:
				getSteps().clear();
				getSteps().addAll((Collection<? extends Step>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case IotaPackage.PROOF__SEQUENT:
				setSequent((Sequent)null);
				return;
			case IotaPackage.PROOF__STEPS:
				getSteps().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case IotaPackage.PROOF__SEQUENT:
				return getSequent() != null;
			case IotaPackage.PROOF__STEPS:
				return steps != null && !steps.isEmpty();
		}
		return super.eIsSet(featureID);
	}

} // Proof
